/********************************************************************************
 * Copyright (c)2016-2020, ShenZhen WanJiaAn Technology CO.,LTD.
 * All Rights Reserved.
 *
 * File Name     : DevManager.c
 * Author        : joki.zhu
 * Date          : 2019-10-16
 *
 * Record        :
 * 1.Date        : 2019-10-16
 *   Author      : joki.zhu
 *   Modification: Created file
 ********************************************************************************/
#include "app_common.h" 
#include "DevManager.h"
#include "DevWifi.h"
#include "DevSd.h"
#include "DevGpio.h"
#include "DevAdc.h"
#include "DevFlash.h"
#include "config_management.h"
#include "Audio.h"
#include "System.h"
#include "DevConfig.h"
#include "DevTemperature.h"
#include "DevWdg.h"
#include "DevSocSub.h"

int DevInit(void)
{
    int ret = 0;
    
    ret = GpioInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "GpioInit");

    ret = SocSubInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "GpioInit");
    
    ret = LedInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "LedInit");
    
    ret = KeyInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "KeyInit");
    
    ret = WifiInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "WifiInit");
    
    ret = SdInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "SdInit");

#ifndef MINI_APP    
    ret = AdcInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "AdcInit");
#endif /* MINI_APP */

    ret = FlashInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "FlashInit");

#ifdef TEMPERATURE_ENABLE
	ret = TemperatureInit();
	AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "TemperatureInit");

#endif /* TEMPERATURE_ENABLE */

    ret = WdgInit();
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "WdgInit");

    return 0;
}


/**
 * @brief 设置从配置获取的参数到设备
 *
 * @param 无
 * 
 * @return |0|成功| |非0|失败|
 *
 * @attention 每次开机都会调用一次,从配置中获取配置,再设置到设备
 */
int DevSetParamsFromCfg(void)
{
    int ret = 0;
    
    /* set pir state */
    char pirSwitch = 0;
    DevConfigGetPirSwitch(&pirSwitch);
    ret = SocSubSetPirSwitch(pirSwitch);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "SocSubSetPirSwitch");

#if 0
    /* set ir night mode */
    int irNightMode = 0;
    DevConfigGetIrNightMode(&irNightMode);
    ret = DevConfigSetIrNightMode(irNightMode);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "DevConfigSetIrNightMode");
#endif

    /* set mcu detect soc */
    ret = SocSubSetMainSocStartDetectTime(10);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "SocSubSetMainSocStartDetectTime");


    /* print mcu version */
    char mcuVersion[32] = "";
    char configMcuVersion[32] = "";
    ret = SocSubGetVersion(mcuVersion);
    AV_RETURN_WITH_INFO_IF_EXPR_FALSE(ret == 0, -1, "SocSubGetVersion");

    DevConfigGetMcuVersion(configMcuVersion, sizeof(configMcuVersion));
    if (strcmp(mcuVersion, configMcuVersion) != 0) {
        DevConfigSetMcuVersion(mcuVersion);
    }
    
    LOGI("mcu version: %s\n", mcuVersion);
    
    return 0;
}

int DevGetUuid(char *pUuid)
{
    AV_RETURN_IF_PTR_NULL(pUuid, AV_FAILURE);

#ifdef DEV_CONFIG
    DevConfigGetUuid(pUuid, 32);
    if (strcmp(pUuid, "0000000000000000") == 0) {
        sprintf(pUuid, "%s", "");
        return ConfigGetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_UUID, pUuid);
    }
#else
    return ConfigGetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_UUID, pUuid);
#endif
}

int DevSetUuid(char *pUuid)
{
    AV_RETURN_IF_PTR_NULL(pUuid, AV_FAILURE);

    int ret = 0;
#ifdef DEV_CONFIG
    ret = DevConfigSetUuid(pUuid);
#else
    ret = ConfigSetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_UUID, pUuid);
    ConfigSave();
#endif

    return ret;
}

int DevGetSoftVersion(char *pVersion)
{
    AV_RETURN_IF_PTR_NULL(pVersion, AV_FAILURE);

#ifdef DEV_CONFIG
    DevConfigGetSoftVersion(pVersion, 32);
    if (strlen(pVersion) == 0) {
        return ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_VERSION, pVersion);
    }
#else
    return ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_VERSION, pVersion);
#endif /* DEV_CONFIG */
}

int DevSetSoftVersion(char *pVersion)
{
    AV_RETURN_IF_PTR_NULL(pVersion, AV_FAILURE);

    int ret = 0;

#ifdef DEV_CONFIG
    ret = DevConfigSetSoftVersion(pVersion);
#else
    ret = ConfigSetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_VERSION, pVersion);
    ConfigSave();
#endif /* DEV_CONFIG */

    return ret;
}

int DevGetPackTime(char *pPackTime)
{
    AV_RETURN_IF_PTR_NULL(pPackTime , AV_FAILURE);
    
#ifdef DEV_CONFIG
    DevConfigGetPackTime(pPackTime, 16);
    if (strlen(pPackTime) == 0) {
        return ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_PACKTIME, pPackTime);
    }
#else
    return ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_PACKTIME, pPackTime);
#endif /* DEV_CONFIG */
}

int DevSetPackTime(char *pPackTime)
{
    AV_RETURN_IF_PTR_NULL(pPackTime, AV_FAILURE);

    int ret = 0;

#ifdef DEV_CONFIG
    ret = DevConfigSetPackTime(pPackTime);
#else
    ret = ConfigSetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_PACKTIME, pPackTime);
    ConfigSave();
#endif /* DEV_CONFIG */

    return ret;
}

int DevGetHardVersion(char *pVersion)
{
    AV_RETURN_IF_PTR_NULL(pVersion, AV_FAILURE);

    return 0;
}

int DevGetProductModel(char *pModel, int nSize)
{
    AV_RETURN_IF_PTR_NULL(pModel, AV_FAILURE);
    AV_RETURN_IF_EXPR_FALSE(nSize > 0, AV_FAILURE);
    
	if (PRODUCT_MODEL_B1 == ProductGetHandle()->ProductModel) {
        snprintf(pModel, nSize, "%s", "B1");
	} else if (PRODUCT_MODEL_B5 == ProductGetHandle()->ProductModel) {
        snprintf(pModel, nSize, "%s", "B5");
	}
	
	return 0;
}

int DevGetNumber(char *pNumber, int nSize)
{
    AV_RETURN_IF_PTR_NULL(pNumber, AV_FAILURE);
    AV_RETURN_IF_EXPR_FALSE(nSize > 0, AV_FAILURE);

	//
	return 0;
}

#ifndef DEV_CONFIG
int DevGetAlexaId(char *pAlexaid)
{
    AV_RETURN_IF_PTR_NULL(pAlexaid, AV_FAILURE);

	int nret = 0;
	nret = ConfigGetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_ALEXAID, pAlexaid);
	if( 0 > nret || pAlexaid[0] == '0' || pAlexaid[0] == 0 ) {
        return -1;
	}
    return 0;
}

int DevSetAlexaId(char *pAlexaid)
{
    AV_RETURN_IF_PTR_NULL(pAlexaid, AV_FAILURE);
    
    int ret = 0;
    ret = ConfigSetEnv(ENV_FACTORY_TOKEN, ENV_FACTORY_KEY_ALEXAID, pAlexaid);
    
    ConfigSave();
    
    return ret;
}

int DevGetTimeZone(char *pTimezone)
{
    AV_RETURN_IF_PTR_NULL(pTimezone, AV_FAILURE);

    int nret = 0;    
	nret = ConfigGetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_TIMEZONE, pTimezone);
    if (nret < 0 || strlen(pTimezone) == 0) {
        return -1;
    }
    
    return nret;
}

int DevSetTimeZone(char *pTimezone)
{
    AV_RETURN_IF_PTR_NULL(pTimezone, AV_FAILURE);

    int ret = 0;
    ret = ConfigSetEnv(ENV_SOFTWARE_TOKEN, ENV_SOFTWARE_KEY_TIMEZONE, pTimezone);
    
    ConfigSave();
    return ret;
}
#endif /* DEV_CONFIG */

int DevDeinit(void)
{
    GpioDeinit();
    WifiDeinit();
    SdDeinit();
    
    return 0;
}

